#include "UserInterface_PCH.h"
#include "UserInterface.h"

/*--------------------------------------------------------------------------*/

IUserInterface* g_UserInterface = NULL;

/*--------------------------------------------------------------------------*/
	
CUserInterface::CUserInterface()
{
	g_Renderer->AddTypeRenderer(this, "ui", 0);
	m_messages_mutex = g_Kernel->CreateNewMutex();
}

/*--------------------------------------------------------------------------*/

CUserInterface::~CUserInterface()
{
	delete m_messages_mutex;
}

/*--------------------------------------------------------------------------*/

void CUserInterface::RenderType( uint arg )
{
	switch (arg)
	{
	case 0:	//ui
		{
			RenderUserInterface();
			break;
		}
	}	
}

/*--------------------------------------------------------------------------*/

void CUserInterface::RenderUserInterface()
{
	g_Renderer->ResetMatrices();
	g_Renderer->SetCamera(GLM_MAT4x4(), m_uiMatrix, -1.0f, 1.0f, glm::vec3());

	RenderMessages();

	//RMaterial* m = g_Renderer->GetRendererStorageMgr()->GetMaterial("default_all");
	//g_Renderer->BindMaterial(m);
}

/*--------------------------------------------------------------------------*/

void CUserInterface::InitializeUI( int x, int y )
{
#ifdef USE_GLM_SIMD
	m_uiMatrix = glm::detail::fmat4x4SIMD(glm::ortho(0.0f, float(x), 0.0f, float(y), -1.0f, 1.0f));
#else
	m_uiMatrix = glm::ortho(0.0f, float(x), 0.0f, float(y), -1.0f, 1.0f);
#endif
}

/*--------------------------------------------------------------------------*/

void CUserInterface::AddMessage( const std::string& msg, const glm::vec4& color )
{
	m_messages_mutex->Lock();

	m_messages.push_back(OnScreenMessage());
	OnScreenMessage& osm = m_messages.back();

	osm.m_color = color;
	osm.m_message = msg;

	m_messages_mutex->Unlock();
}

/*--------------------------------------------------------------------------*/

void CUserInterface::RenderMessages()
{
	m_messages_mutex->Lock();

	TextMgr* textMgr = g_Renderer->GetTextMgr();
	glm::vec2 position = glm::vec2(10.0f, 20.0f);

	for (auto iter = m_messages.begin(); iter != m_messages.end(); ++iter)
	{
		textMgr->SetTextColor(iter->m_color);
		textMgr->WriteText(NULL, iter->m_message, position);

		position.y += 20.0f;
	}

	m_messages.clear();

	m_messages_mutex->Unlock();
}

/*--------------------------------------------------------------------------*/
